package com.shqcjd.preinstallglass.ui.activity.remote;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.shqcjd.preinstallglass.R;
import com.shqcjd.preinstallglass.api.ApiService;
import com.shqcjd.preinstallglass.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallglass.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallglass.model.ResponseData;
import com.shqcjd.preinstallglass.model.pojo.ExpertListBean;
import com.shqcjd.preinstallglass.ui.adapter.ExpertListAdapter;
import com.shqcjd.preinstallglass.ui.base.BaseActivity;
import com.shqcjd.preinstallglass.ui.base.BasePresenter;
import com.shqcjd.preinstallglass.util.SpacesItemDecoration;
import com.shqcjd.preinstallglass.util.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class ExpertListActivity extends BaseActivity {


    @BindView(R.id.rl_expert_list)
    RecyclerView rlExpertList;
    @BindView(R.id.ib_back)
    Button ibBack;
    private ExpertListAdapter expertListAdapter;
    private Context mContext;
    private int expertType;
    private List<ExpertListBean.ItemsBean> mList = new ArrayList<>();
    private boolean isFirst = true;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_expert_list;
    }

    @Override
    public void initView() {
        mContext = this;
        //结束全程录像--每次调用相机前都需要调用该方法

        expertType = getIntent().getIntExtra("expertType", 0);
        initAdapter();

    }

    private void initAdapter() {
        expertListAdapter = new ExpertListAdapter();
        rlExpertList.setLayoutManager(new LinearLayoutManager(this));
        rlExpertList.addItemDecoration(new SpacesItemDecoration(16, 13));
        rlExpertList.setAdapter(expertListAdapter);
        expertListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ExpertListBean.ItemsBean item = (ExpertListBean.ItemsBean) adapter.getItem(position);
                Intent intent = new Intent(mContext, VideoCallActivity.class);
                intent.putExtra("expertId", item.id);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
    }

    @Override
    protected boolean isAutomaticInitVoice() {
        return false;
    }


    @Override
    protected void onVoiceResult(String result) {
        Logger.i("识别结果----:"+result);
        if(result.equals("返回")){
            finish();
        }else{
            voiceResult(result);
        }
    }

    //"|返回|保存|拍照|输入|删除;"
    private String addVoiceContent(){
        String str = "";
        for(int i=1; i<=mList.size(); i++){
            str = str + "|选择"+i;
        }
        return str + "|返回;";
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!isFirst){
            getVoiceContent(addVoiceContent());
        }
        if(isFirst){
            isFirst = false;
        }
    }

    private void voiceResult(String result){
        for(int i=1; i<=mList.size(); i++){
            if(result.equals("选择"+i)){
                ExpertListBean.ItemsBean item = (ExpertListBean.ItemsBean) mList.get(i-1);
                Intent intent = new Intent(mContext, VideoCallActivity.class);
                intent.putExtra("expertId", item.id);
                startActivity(intent);
                return;
            }
        }
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageIndex", "1");
        map.put("pageSize", "10000");
        map.put("expert_type", ""+expertType);//专家类型 1专家 2专工 3供应商 0全部
        map.put("expert_name", "");

        ApiService.getExpertList(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ExpertListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<ExpertListBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            mList = retData.data.items;
                            expertListAdapter.setNewData(mList);
                            getVoiceContent(addVoiceContent());
                        } else {
                            ToastUtils.showShort(ExpertListActivity.this, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        ToastUtils.showShort(ExpertListActivity.this, errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @OnClick(R.id.ib_back)
    public void onViewClicked() {
        finish();
    }
}
